package com.jxs.android.weixi;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.jxs.android.soap.SoapUtil;
import com.jxs.android.util.KeyboardUtil;
import com.jxs.android.util.StringUtils;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.ClickAbleText;
import com.jxs.android.weiget.RefreshListView;
import com.jxs.android.weiget.TopBar;
import com.jxs.android.weixi.data.OweFeeResultInfo;
import com.telecom.weixi.R;

/**
 * @Author jiangxs
 * @Des
 */
public class Search_Of_OweFee extends LoadingMoreAbstractList implements OnClickListener {

	private final int noticePageSize = 15;
	private NoticeListInfoAdapter adapter;
	private View bu_search_list_header;
	private EditText search_phone, search_user;
	private ImageButton search_phone_btn, search_user_btn;
	private TextView top_total_bar;
	protected ImageView backtop;

	@Override
	public int getLayout() {
		return R.layout.bu_search_user;
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		topBar.hindBtn();
		topBarReturn(topBar);
		topBar.setTitle(R.string.search_owe);
	}

	@Override
	protected void addListHeadView(RefreshListView refreshListView) {
		super.addListHeadView(refreshListView);

		top_total_bar = (TextView) findViewById(R.id.top_total_bar);
		top_total_bar.setOnClickListener(this);
		top_total_bar.setVisibility(View.GONE);
		bu_search_list_header = LayoutInflater.from(this).inflate(R.layout.bu_search_list_header, null);

		search_phone = (EditText) bu_search_list_header.findViewById(R.id.bu_search_phone);
		search_user = (EditText) bu_search_list_header.findViewById(R.id.bu_search_user);

		search_user_btn = (ImageButton) bu_search_list_header.findViewById(R.id.bu_search_user_btn);
		search_phone_btn = (ImageButton) bu_search_list_header.findViewById(R.id.bu_search_phone_btn);

		search_user_btn.setOnClickListener(this);
		search_phone_btn.setOnClickListener(this);

		refreshListView.addHeaderView(bu_search_list_header, null, false);
		backTop();
	}

	@Override
	protected boolean isOnCreatLoading() {
		return false;
	}

	private Which which = Which.OWE_FEE_SEARCH_NAME;
	private String editValue = "";

	@Override
	public void onClick(View v) {
		currentPage = 1;
		if (v.getId() == search_user_btn.getId()) {
			which = Which.OWE_FEE_SEARCH_NAME;
			editValue = search_user.getEditableText().toString();
			if (StringUtils.isEmpety(editValue)) {
				errorMsgPop(R.string.bu_search_user_name);
				return;
			}
			loadingData(currentPage, true);
		} else if (v.getId() == search_phone_btn.getId()) {
			which = Which.OWE_FEE_SEARCH_PHONE;
			editValue = search_phone.getEditableText().toString();
			if (StringUtils.isEmpety(editValue)) {
				errorMsgPop(R.string.bu_search_user_phone);
				return;
			}
			loadingData(currentPage, true);
		} else if (v.getId() == top_total_bar.getId()) {
			refreshListView.setSelection(1);
		}
	}

	@Override
	public void loadingData(int currentPage, boolean showDialog) {
		if (StringUtils.isEmpety(editValue)) {
			errorMsgPop(R.string.bu_search_user_null);
			refreshListView.onRefreshComplete();
			return;
		}

		if (KeyboardUtil.isImeShow(this)) {
			KeyboardUtil.hideIme(this);
		}

		PAGE_SIZE = noticePageSize;
		getWebServiceData(WeixiApplication.getUserInfo().getTicket(), which, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();

			}

		}, editValue, PAGE_SIZE, currentPage);
	}

	@Override
	public BaseAdapter getAdapter(Context context) {
		adapter = new NoticeListInfoAdapter(this);
		return adapter;
	}

	@Override
	public List<OweFeeResultInfo> paserData(SoapObject retMsgObject, Object... params) {
		List<OweFeeResultInfo> searchUserInfos = new ArrayList<OweFeeResultInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				OweFeeResultInfo termInfo = new OweFeeResultInfo();
				termInfo = new OweFeeResultInfo();
				termInfo.acc_nbr = SoapUtil.getValue(taskBeanObject, "acc_nbr");//
				termInfo.serv_id = SoapUtil.getValue(taskBeanObject, "serv_id");//
				termInfo.cust_id = SoapUtil.getValue(taskBeanObject, "cust_id");//
				termInfo.latn_name = SoapUtil.getValue(taskBeanObject, "latn_name");//
				termInfo.total_owe_charge = SoapUtil.getValue(taskBeanObject, "total_owe_charge");//
				termInfo.total_owe_month = SoapUtil.getValue(taskBeanObject, "total_owe_month");//

				searchUserInfos.add(termInfo);
			}
		}
		return searchUserInfos;
	}

	@Override
	public void updateList(Message msg) {

		if (null != msg.getData()) {
			if (!StringUtils.isEmpety(msg.getData().getString("totalRows"))) {
				top_total_bar.setVisibility(View.VISIBLE);
				top_total_bar.setText("记录总数:【" + msg.getData().getString("totalRows") + " 】条");
			}

		}

		@SuppressWarnings("unchecked")
		List<OweFeeResultInfo> tems = ((List<OweFeeResultInfo>) msg.obj);
		refreshListView.onRefreshComplete();
		empytText.setVisibility(View.GONE);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				empytText.setVisibility(View.VISIBLE);
				loadMoreButton.setText("数据已全部获取完毕");
				loadMoreButton.setEnabled(false);
			}
		}
		adapter.addOweFeeResultInfos(tems);
		adapter.notifyDataSetChanged();
		if (tems.size() < PAGE_SIZE) {
			loadMoreButton.setText("数据已全部获取完毕");
			loadMoreButton.setEnabled(false);
		} else {
			loadMoreButton.setText("查看更多...");
			currentPage++;
		}
		if (adapter.getCount() > 5) {
			backtop.setVisibility(View.VISIBLE);
		} else {
			backtop.setVisibility(View.INVISIBLE);
		}
	}

	private class NoticeListInfoAdapter extends BaseAdapter {
		private List<OweFeeResultInfo> oweFeeResultInfos = new ArrayList<OweFeeResultInfo>();
		private Context context;

		public void addOweFeeResultInfos(List<OweFeeResultInfo> searchUserInfos) {
			if (currentPage == 1) {
				this.oweFeeResultInfos.clear();
			}
			this.oweFeeResultInfos.addAll(searchUserInfos);
		}

		public NoticeListInfoAdapter(Context context) {
			this.context = context;
		}

		public int getCount() {
			return oweFeeResultInfos.size();
		}

		public OweFeeResultInfo getItem(int position) {
			return oweFeeResultInfos.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final OweFeeResultInfo oweFeeResultInfo = oweFeeResultInfos.get(position);
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.bu_search_owe_fee_view_result_item, null);
				holder = new ViewHolder();
				holder.position = (TextView) convertView.findViewById(R.id.position);// 18919889828
				holder.bu_search_owefee_phone = (ClickAbleText) convertView.findViewById(R.id.bu_search_owefee_phone);// 18919889828
				holder.bu_search_owefee_phone.setClickAble(true);
				holder.bu_search_owefee_latn_name = (TextView) convertView.findViewById(R.id.bu_search_owefee_latn_name);// 璐��ARPU:109.98
				holder.bu_search_owefee_total_owe_charge = (TextView) convertView.findViewById(R.id.bu_search_owefee_total_owe_charge);// 璐��ARPU:109.98
				holder.bu_search_owefee_total_owe_month = (TextView) convertView.findViewById(R.id.bu_search_owefee_total_owe_month);// �ㄦ�ARPU:47.48
				holder.item_arrow = (ImageView) convertView.findViewById(R.id.item_arrow);// 姝ｅ父

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.position.setText("No." + (position + 1));// 18919889828
			holder.bu_search_owefee_phone.setText(oweFeeResultInfo.acc_nbr);
			holder.bu_search_owefee_latn_name.setText(oweFeeResultInfo.latn_name);
			holder.bu_search_owefee_total_owe_charge.setText(oweFeeResultInfo.total_owe_charge);
			holder.bu_search_owefee_total_owe_month.setText(oweFeeResultInfo.total_owe_month);
			View.OnClickListener listener = new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					onclickView(oweFeeResultInfo);
				}
			};
			holder.bu_search_owefee_phone.setOnClickListener(listener);
			holder.item_arrow.setOnClickListener(listener);
			return convertView;
		}

		private void onclickView(OweFeeResultInfo oweFeeResultInfo) {
			Intent intent = new Intent(context, Search_Of_OweFee_Detail.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("oweFeeResultInfo", oweFeeResultInfo);
			intent.putExtras(bundle);
			startActivity(intent);
		}

		class ViewHolder {
			TextView position;// position
			ClickAbleText bu_search_owefee_phone;
			TextView bu_search_owefee_latn_name;
			TextView bu_search_owefee_total_owe_charge;
			TextView bu_search_owefee_total_owe_month;
			ImageView item_arrow;//
		}
	}

	@Override
	public String getMethod() {

		if (which.ordinal() == Which.OWE_FEE_SEARCH_NAME.ordinal()) {
			return "getOweInfoByUserName";
		} else if (which.ordinal() == Which.OWE_FEE_SEARCH_PHONE.ordinal()) {
			return "getOweInfoByAccNbr";
		}
		return "";

	}

	private void backTop() {
		backtop = (ImageView) findViewById(R.id.back_top);
		backtop.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View paramView) {
				refreshListView.setSelection(1);
			}
		});
	}

	void laucherListScorll() {
		refreshListView.setOnScrollListener(new AbsListView.OnScrollListener() {

			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				if (firstVisibleItem > 5) {
					backtop.setVisibility(View.VISIBLE);
				} else {
					backtop.setVisibility(View.INVISIBLE);
				}
			}
		});
	}

}
